// ----------------------------------------------------------------------------
// byte_writer.cpp
// ----------------------------------------------------------------------------
#include <fw/base/serialize/byte_writer.h>
#include <fw/base/core/assertion.h>

namespace fw
{
	ByteWriter::ByteWriter(char* data, size_t size)
	{
		SetBuffer(data, size);
	}
	
	ByteWriter::ByteWriter()
	{
		SetBuffer(nullptr, 0);
	}
	
	void ByteWriter::SetBuffer(char *data, size_t size)
	{
		m_data	 = data;
		m_size	 = size;
		m_offset = 0;
	}
	
	char* ByteWriter::GetBuffer() const
	{
		return m_data;
	}
	
	void ByteWriter::Seek(size_t pos)
	{
		m_offset = pos;
	}
	
	size_t ByteWriter::Tell() const
	{
		return m_offset;
	}
	
	void ByteWriter::Write8(uint8_t value)
	{
		if(m_data)
		{
			FW_ASSERT(m_offset<m_size);
			m_data[m_offset] = value;
		}
		m_offset += 1;
	}
	
	void ByteWriter::Write16(uint16_t value)
	{
		Write8(static_cast<uint8_t>(value>>8));
		Write8(static_cast<uint8_t>(value   ));
	}
	
	void ByteWriter::Write32(uint32_t value)
	{
		Write16(static_cast<uint16_t>(value>>16));
		Write16(static_cast<uint16_t>(value    ));
	}

	void ByteWriter::Write64(uint64_t value)
	{
		Write32(static_cast<uint32_t>(value>>32));
		Write32(static_cast<uint32_t>(value    ));
	}
	
} // namespace fw
